/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service.impl;

import eao.EaoLocal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import model.entity.Task;
import model.entity.TaskCustomerdemand;
import service.ITaskmanager;
import service.InterfaceServiceLocal;

/**
 *
 * @author dingnangen
 */
@Stateless

public class TaskManager extends AbstractService<Task> implements ITaskmanager<Task> {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB(beanName = "TaskFacade")
    eao.EaoLocal<Task> taskFacade;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    protected EaoLocal getEao() {
        return taskFacade;
    }

    @Override
    public Task getTaskByLoginName(String loginname, String date) {

        final String qs = "select t from Task t where t.idattendance.iddriver.iduser.loginname=?1 and t.idattendance.attendancedate=?2 and t.idsolution.operationrecord.state=1";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date _date;
        if (date == null) {
            _date = new Date();
        } else {
            try {
                _date = simpleDateFormat.parse(date);
            } catch (ParseException ex) {
                _date = new Date();
                Logger.getLogger(TaskManager.class.getName()).log(Level.SEVERE, "日期转换失败", ex);
            }
        }
        ArrayList pList = new ArrayList();
        pList.add(loginname);
        pList.add(_date);
        List<Task> taskList = this.findAll(qs, pList);
        System.out.println("TaskManager:getTaskByLoginName:"+taskList);
        Task task = null;
        if (taskList != null&&taskList.size()>0) {
            task = taskList.get(0);
            System.out.println("TaskManager:" + loginname + _date + "," + task.getIdtask());
            List<TaskCustomerdemand> taskCustomerdemandList = new ArrayList(task.getTaskCustomerdemandList());
            Collections.sort(taskCustomerdemandList, new TaskCustomerdemandComparator());
            task.setTaskCustomerdemandList(taskCustomerdemandList);
        }
        return task;
    }

    @Override
    public String TaskCommit(String idcustomerdemand, String date) {
        return null;
    }

    class TaskCustomerdemandComparator implements Comparator<TaskCustomerdemand> {

        @Override
        public int compare(TaskCustomerdemand o1, TaskCustomerdemand o2) {
            return o1.getVisiteorder() - o2.getVisiteorder();
        }
    }

}
